#include "OssUploader.hpp"
#include <iostream>
using std::cout;
using std::endl;
using std::string;

#include <nlohmann/json.hpp>

#include <SimpleAmqpClient/SimpleAmqpClient.h>
using AmqpClient::BasicMessage;
using AmqpClient::Channel;
using AmqpClient::Envelope;

// g++ Consumer.cc -o Consumer -lSimpleAmqpClient -lalibabacloud-oss-cpp-sdk -lwfrest -lworkflow -lssl -lcrypto -lcrypt -lcurl -lpthread

class Consumer
{
public:
	Consumer(const string &queuename)
		: _channel(Channel::Create())
	{
		// 以阻塞方式启动一个消费者
		_channel->BasicConsume(queuename, queuename);
	}

	void doConsume()
	{
		while (1)
		{
			// 创建一个信封，用来存储消息
			Envelope::ptr_t envelope;
			// 5000ms超时时间
			bool flag = _channel->BasicConsumeMessage(envelope, 5000);
			if (flag)
			{
				string msg = envelope->Message()->Body();
				cout << "msg:" << msg << endl;
				using Json = nlohmann::json;
				Json uploadInfo = Json::parse(msg);
				string filepath = uploadInfo["filePath"];
				string objectName = uploadInfo["objectName"];
				_ossUploader.doUploadFile(objectName, filepath);
			}
			else
			{
				cout << "timeout." << endl;
			}
		}
	}

private:
	OssUploader _ossUploader;
	Channel::ptr_t _channel;
};

void test0()
{
	Consumer consumer("oss_queue");
	consumer.doConsume();
}

int main()
{
	test0();
	return 0;
}
